package com.example.controller.wechat;

import cn.hutool.core.util.StrUtil;
import com.example.pojo.web.Result;
import com.example.pojo.wechat.BrokenBike;
import com.example.service.wechat.BrokenBikeService;
import com.example.utils.ResultUtil;
import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.GeometryFactory;
import org.locationtech.jts.geom.Point;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @ClassName BrokenBikeController
 * @Description: com.example.controller.wechat
 * @Auther: xiwd
 * @Date: 2022/4/19 - 04 - 19 - 11:58
 * @version: 1.0
 */
@Controller
@RequestMapping(value = "/brokenbike")
public class BrokenBikeController {
    //properties
    @Autowired
    private BrokenBikeService brokenBikeService;

    //methods

    /**
     * 查询所有的损坏单车记录
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/select/all")
    public Result selectBrokenBikesAll(){
        try {
            return ResultUtil.success(1,"查询成功",brokenBikeService.selectBrokenBikesAll());
        }catch (Exception e){
            e.printStackTrace();
            return ResultUtil.error(-1,"系统异常");
        }
    }


    /**
     * 添加损坏单车记录
     * @param address
     * @param description
     * @param photo
     * @param code
     * @param longitude
     * @param latitude
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/insert/record")
    public Result addBrokenBikeRecord(@RequestParam(value = "address")String address,
                                      @RequestParam(value = "description")String description,
                                      @RequestParam(value = "photo")MultipartFile photo,
                                      @RequestParam(value = "code")String code,
                                      @RequestParam(value = "longitude")Double longitude,
                                      @RequestParam(value = "latitude")Double latitude){
        try {
            if (StrUtil.isEmpty(code)||photo==null)
                return ResultUtil.error(0,"上报失败");
            //解析地址信息
            GeometryFactory geometryFactory = new GeometryFactory();
            Point location = geometryFactory.createPoint(new Coordinate(latitude, longitude));
            //创建Broken对象
            BrokenBike brokenBike = new BrokenBike();
            brokenBike.setAddress(address);
            brokenBike.setDescription(description);
            brokenBike.setLocation(location.toString());
            brokenBike.setPhoto(photo.getBytes());
            brokenBike.setUploader(code);
            System.out.println(brokenBike);
            //调用方法-生成订单
            Integer rows = brokenBikeService.addBrokenBikeRecord(brokenBike,code);
            if (rows>0){
                return ResultUtil.success(1,"上报成功");
            }else {
                return ResultUtil.error(0,"上报失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            return ResultUtil.error(-1,"系统异常");
        }
    }

    /**
     * 根据id处理损坏单车记录
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/update")
    public Result updateBrokenBikeByID(@RequestParam(value = "id") Integer id){
        return brokenBikeService.updateBrokenBikeByID(id);
    }

}
